package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import welsir.entity.dos.Disease;
import welsir.entity.dos.SeedingDisease;
import welsir.entity.vo.DiseasePageVO;
import welsir.entity.vo.ResultMessage;
import welsir.mapper.DiseaseMapper;
import welsir.mapper.SeedingDiseaseMapper;
import welsir.service.IDiseaseService;
import welsir.util.MathUtil;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author welsir
 * @date 2023/3/8 1:43
 */
@RestController
@RequestMapping("/disease")
@Api(value = "疾病防护Controller")
public class DiseaseController {

    @Resource
    private DiseaseMapper diseaseMapper;

    @Resource
    private IDiseaseService diseaseService;

    @Resource
    private SeedingDiseaseMapper seedingDiseaseMapper;

    @ApiOperation(value = "获取所有疾病防护类型分页")
    @GetMapping("getAllDiseaseByPage/{page}/{size}")
    public ResultMessage<DiseasePageVO> getAllDiseaseByPage(
            @PathVariable Integer page,
            @PathVariable Integer size
    ){
        return ResultUtil.data(this.diseaseService.VoList(page,size));
    }

    @ApiOperation(value = "获取所有疾病防护类型")
    @GetMapping("getAllDisease")
    public ResultMessage<List<Disease>> getAllDisease(){
        return ResultUtil.data(this.diseaseService.list());
    }

    @ApiOperation(value = "新增疾病防护类型")
    @PostMapping("insertDiseaseType")
    public ResultMessage<Integer> insertDiseaseType(@RequestBody Disease disease){
        disease.setId(((Integer)MathUtil.randomDigitNumber(7)).longValue());
        return ResultUtil.data(this.diseaseMapper.insert(disease));
    }
    @ApiOperation(value = "修改疾病防护类型")
    @PostMapping("updateDiseaseMsg")
    public ResultMessage<Integer> updateDiseaseMsg(@RequestBody Disease disease){
        return ResultUtil.data(this.diseaseMapper.updateById(disease));
    }
    @ApiOperation(value = "根据uid删除疾病防护类型")
    @GetMapping("deleteDisease/{id}")
    public ResultMessage<Integer> deleteDisease(@PathVariable String id){
        seedingDiseaseMapper.delete(new QueryWrapper<SeedingDisease>().eq("disease_id",id));
        return ResultUtil.data(this.diseaseMapper.delete(new QueryWrapper<Disease>().eq("id",id)));
    }
}
